<?php
class ThanhtichcbsController extends AppController
{

	var $name = 'Thanhtichcbs';
	var $uses= array('Thanhtichcb','Hockie','Tgiachidoancb');

	function index($phongtrao_id=null)
    {
    	if(!$phongtrao_id)
    		$this->redirect(array('controller'=>'pages','action'=>'home'));
    	$phongtrao = $this->Thanhtichcb->Phongtraocb->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraocb.id'=>$phongtrao_id)));
    	$chidoan_id = $phongtrao['Phongtraocb']['chidoansv_id'];

    	// lay sinh vien thuoc chi doan
    	$canbos = $this->Tgiachidoancb->find('list',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>null,'chidoansv_id'=>$chidoan_id),'fields'=>array('Canbo.id','Canbo.hoten')));
    	
		$this->Thanhtichcb->recursive = 0;
		$this->set('thanhtichcbs', $this->paginate(array('phongtraocb_id'=>$phongtrao_id)));
		$this->set(compact('phongtrao','canbos'));
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thanhtichcb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thanhtichcb', $this->Thanhtichcb->read(null, $id));
	}

	function add($phongtrao_id)
	{
		if (!empty($this->data))
		{
			$this->Thanhtichcb->create();
			
			$this->data['Thanhtichcb']['phongtraocb_id'] = $phongtrao_id;			
			if ($this->Thanhtichcb->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'add',$phongtrao_id));
			}
			
			else
			{
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		$this->Thanhtichcb->recursive = 0;
		$this->set('thanhtichcbs', $this->paginate(array('phongtraocb_id'=>$phongtrao_id)));
		$phongtrao = $this->Thanhtichcb->Phongtraocb->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraocb.id'=>$phongtrao_id)));
    	$chidoan_id = $phongtrao['Phongtraocb']['chidoansv_id'];
    	$cbdaco = $this->Thanhtichcb->find('list',array('conditions'=>array('phongtraocb_id'=>$phongtrao_id),'fields'=>array('canbo_id')));
   
    	$cbs = $this->Tgiachidoancb->Canbo->find('all',array('recursive'=>'0',
    														'conditions'=>array('NOT'=>array('Canbo.id'=>$cbdaco)),'fields'=>array('Canbo.id','Canbo.mscb','Canbo.hoten')));
		foreach ($cbs as $cb)
    	{
    		$canbos[$cb['Canbo']['id']]=$cb['Canbo']['mscb'].'-'.$cb['Canbo']['hoten'];
    	}
    	
		$this->set(compact('canbos', 'phongtrao','phongtrao_id'));
		
		
	}

	function edit($id = null,$phongtrao_id)
	{
		
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		
		if (!empty($this->data))
		{
			if ($this->Thanhtichcb->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$phongtrao_id));
			} else {
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		if (empty($this->data))
		{
			$this->data = $this->Thanhtichcb->read(null, $id);
		}
		$phongtrao_id=$this->data['Thanhtichcb']['phongtraocb_id'];
		$phongtrao = $this->Thanhtichcb->Phongtraocb->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraocb.id'=>$phongtrao_id)));
    	$chidoan_id = $phongtrao['Phongtraocb']['chidoansv_id'];
    	$canbo_id = $this->data['Thanhtichcb']['canbo_id'];
    	$cbdaco = $this->Thanhtichcb->find('list',array('conditions'=>array('phongtraocb_id'=>$phongtrao_id,'NOT'=>array('canbo_id'=>$canbo_id)),'fields'=>array('canbo_id')));
		$cbs = $this->Tgiachidoancb->Canbo->find('all',array('recursive'=>'0',
    														'conditions'=>array('NOT'=>array('Canbo.id'=>$cbdaco)),'fields'=>array('Canbo.id','Canbo.mscb','Canbo.hoten')));
		foreach ($cbs as $cb)
    	{
    		$canbos[$cb['Canbo']['id']]=$cb['Canbo']['mscb'].'-'.$cb['Canbo']['hoten'];
    	}    	
		$this->set(compact('canbos', 'phongtrao','phongtrao_id'));
		
	}

	function delete($id = null,$phongtrao_id=null)
	{
		if(!$phongtrao_id)
			$this->redirect(array('pages','action'=>'home'));
		if (!$id)
		{
			$this->Session->setFlash(__('giá trị không tồn tại', true));
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		if ($this->Thanhtichcb->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		$this->Session->setFlash(__('Không thể xóa. Vui lòng thử lại', true),'error');
		$this->redirect(array('action' => 'index',$phongtrao_id));
	}

}
